package com.kedou.sdk.ui.assist;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.kedou.sdk.content.IActivityContent;
import com.kedou.sdk.utils.ResUtils;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

public class ActivityListAdapter extends BaseAdapter {

    private final List<IActivityContent> data;
    private LayoutInflater inflater;

    public ActivityListAdapter() {
        data = new ArrayList<>();
    }

    public void resetItemData(List<IActivityContent> data) {
        this.data.clear();
        if (data != null && !data.isEmpty()) {
            this.data.addAll(data);
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public IActivityContent getItem(int position) {
        if (position >= 0 && position < data.size()) {
            return data.get(position);
        }
        return null;
    }

    public List<String> getActivityDetailList() {
        List<String> details = new ArrayList<>();
        for (IActivityContent content : data) {
            details.add(content.getActivityDetailUrl());
        }
        return details;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final Context context = parent.getContext();
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();

            if (inflater == null) {
                inflater = LayoutInflater.from(context);
            }

            final int layoutResId = ResUtils.getLayoutIdentifier(context, "hnssn_item_activity_content");
            final int iconResId = ResUtils.getViewIdentifier(context, "hnssn_item_activity_mark");
            final int titleResId = ResUtils.getViewIdentifier(context, "hnssn_item_activity_title");
            final int contentResId = ResUtils.getViewIdentifier(context, "hnssn_item_activity_image");

            convertView = inflater.inflate(layoutResId, parent, false);
            viewHolder.title = convertView.findViewById(titleResId);
            viewHolder.closureMark = convertView.findViewById(iconResId);
            viewHolder.contentImage = convertView.findViewById(contentResId);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        IActivityContent activityContent = getItem(position);
        if (activityContent != null) {
            if (activityContent.isActivityClosure()) {
                viewHolder.closureMark.setVisibility(View.VISIBLE);
            } else {
                viewHolder.closureMark.setVisibility(View.GONE);
            }
            viewHolder.title.setText(activityContent.getActivityTitle());
            Picasso.with(context).load(activityContent.getActivityImageUrl()).into(viewHolder.contentImage);
        }
        return convertView;
    }


    public static class ViewHolder {
        public TextView title;
        public ImageView closureMark;
        public ImageView contentImage;
    }

}
